package cn.maxwell.result;

import cn.maxwell.enums.GlobalErrorCode;
import cn.maxwell.exception.GlobalCustomException;
import lombok.Data;

import java.math.BigDecimal;
import java.math.BigInteger;

//返回JSON结果
@Data
//建造者模式
//@Builder
public class JsonResult {

    private boolean success = true;

    private String message = "成功";

    //错误码，用来描述错误类型 ，1000 表示么有错误
    private String code = "1000";

    //返回的数据
    private Object data;

    /** 创建当前实例 **/
    public static JsonResult success(){
        return new JsonResult();
    }
    /** 创建当前实例 **/
    public static JsonResult success(Object obj){
        JsonResult instance = new JsonResult();
        instance.setData(obj);
        return instance;
    }

    public static JsonResult success(Object obj, String code){
        JsonResult instance = new JsonResult();
        instance.setCode(code);
        instance.setData(obj);
        return instance;
    }
    /** 创建当前实例 **/

    public static JsonResult error(String message, String code){
        JsonResult instance = new JsonResult();
        instance.setMessage(message);
        instance.setSuccess(false);
        instance.setCode(code);
        return instance;
    }

    /**
     * 自定义异常捕捉优化
     * @param e
     * @return
     */

    public static JsonResult error(GlobalCustomException e){
        JsonResult instance = new JsonResult();
        instance.setMessage(e.getErrorMessage());
        instance.setSuccess(false);
        instance.setCode(e.getErrorCode());
        return instance;
    }

    /**
     * 错误码
     * @param errorCode
     * @return
     */
    public static JsonResult error(GlobalErrorCode errorCode){
        JsonResult instance = new JsonResult();
        instance.setMessage(errorCode.getMessage());
        instance.setSuccess(false);
        instance.setCode(errorCode.getCode());
        return instance;
    }

    public static JsonResult error(){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setSuccess(false);
        return jsonResult;
    }

    /** 创建当前实例 **/
    public static JsonResult error(String message){

        return error(message,null);
    }

    /**
     * Object转BigDecimal类型
     *
     * @param value 要转的object类型
     * @return 转成的BigDecimal类型数据
     */
    public static BigDecimal getBigDecimal(Object value) {
        BigDecimal ret = null;
        if (value != null) {
            if (value instanceof BigDecimal) {
                ret = (BigDecimal) value;
            } else if (value instanceof String) {
                ret = new BigDecimal((String) value);
            } else if (value instanceof BigInteger) {
                ret = new BigDecimal((BigInteger) value);
            } else if (value instanceof Number) {
                ret = new BigDecimal(((Number) value).doubleValue());
            } else {
                throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass() + " into a BigDecimal.");
            }
        }
        return ret;
    }
}
